<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/5/7
 * Time: 下午4:13
 */

class Companys extends Eloquent
{
    protected $table = "companys";

    //判断账期状态
    public static function accountDayStatus($company){
        //账期状态
        $credit_status = array(
            'day_status'=>'逾期（禁止挂帐）',
            'color'=>'red'
        );
        $begin_time = "";
        //未核销
        if(!is_null($company->start_year) && !is_null($company->start_month) && !is_null($company->accounting_cycle) && !is_null($company->collection_cycle) && $company->accounting_cycle != 0){
            $credit_order = Orders::leftJoin('order_pay_record','orders.order_id','=','order_pay_record.order_id')->where('orders.pay_user_id',$company->id)->whereIn('orders.pay_type',[6,9])
                ->where('order_pay_record.pay_type',6)->where('order_pay_record.status',1)->whereIn('orders.status',array(1018,1017,1217,1317,1417))->where('orders.verify_status','0')->orderBy('orders.pay_time','asc')
                ->select('orders.pay_time')->first();
            $credit_order1 = CarWashOrder::leftJoin('car_wash_order_pay_record','car_wash_order.order_id','=','car_wash_order_pay_record.order_id')->whereIn('car_wash_order.pay_type',[6,9])
                ->where('car_wash_order_pay_record.pay_type',6)->where('car_wash_order_pay_record.status',1)->where('car_wash_order.user_id',$company->id)->whereIn('car_wash_order.status',array(1018,1017,1217,1317,1417))->where('car_wash_order.verify_status','0')->orderBy('car_wash_order.pay_time','asc')
                ->select('car_wash_order.pay_time')->first();
            $credit_order2 = ServiceCardOrder::leftJoin('service_card_order_pay_record','service_card_order.order_id','=','service_card_order_pay_record.order_id')->whereIn('service_card_order.pay_type',[6,9])
                ->where('service_card_order_pay_record.pay_type',6)->where('service_card_order_pay_record.status',1)->where('service_card_order.user_id',$company->id)->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))->where('service_card_order.verify_status','0')->orderBy('service_card_order.pay_time','asc')
                ->select('service_card_order.pay_time')->first();
            //期初
            $begin = BeginArrear::where('company_id',$company->id)->where('current_amount','!=',0)->orderBy('updated_at','asc')->first();
            $arr = [];
            $arr[] = isset($credit_order) ? $credit_order->pay_time : null;
            $arr[] = isset($credit_order1) ? $credit_order1->pay_time : null;
            $arr[] = isset($credit_order2) ? $credit_order2->pay_time : null;
            $arr[] = isset($begin) ? $begin->updated_at : null;
            $arr = array_filter($arr);
            if(sizeof($arr) > 0){
                sort($arr);
                $begin_time = $arr[0];
                if($begin_time == ""){
                    $credit_status['day_status'] = "正常";
                    $credit_status['color'] = "";
                }else {
                    $begin_time = strtotime($begin_time);
                    $y_m_time = strtotime($company->start_year . '-' . $company->start_month);
                    $now_time = strtotime(date('Y-m', time()));
                    $be_time = strtotime(date('Y-m', $begin_time));
                    $end_time = Companys::getEndTime($begin_time,$y_m_time,$company->accounting_cycle,$company->collection_cycle);
                    if ($y_m_time <= $be_time && $now_time <= $end_time) {
                        $credit_status['day_status'] = "正常";
                        $credit_status['color'] = "";
                    }
                }
            }else{
                $credit_status['day_status'] = "正常";
                $credit_status['color'] = "";
            }
        }
        return $credit_status;
    }
    //计算时间点所在周期的期末, return int
    public static function getEndTime($begin_time,$y_m_time,$accounting_cycle,$collection_cycle){
        list($date_2['y'], $date_2['m']) = explode("-", date('Y-m',$begin_time));
        list($date_1['y'], $date_1['m']) = explode("-", date('Y-m',$y_m_time));
        $s_diff_num = (floor((abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m']) / $accounting_cycle) + 1) *
            $accounting_cycle - 1 + $collection_cycle;
        return strtotime(date('Y-m',$y_m_time) . ' +' . $s_diff_num . ' month');
    }
}

